﻿using System.Windows;
using Phone7.Fx.Controls;

namespace Wp7nl.Utilities
{
  public static class AppBarBindingHelpers
  {
    #region Attached Dependency Property IsVisible
    public static readonly DependencyProperty IsVisibleProperty =
         DependencyProperty.RegisterAttached("IsVisible",
         typeof(bool),
         typeof(BindableApplicationBar),
         new PropertyMetadata(true, IsVisibleChanged));

    // Called when Property is retrieved
    public static bool GetIsVisible(DependencyObject obj)
    {
      return (bool)obj.GetValue(IsVisibleProperty);
    }

    // Called when Property is set
    public static void SetIsVisible(
       DependencyObject obj,
       bool value)
    {
      obj.SetValue(IsVisibleProperty, value);
    }

    // Called when property is changed
    private static void IsVisibleChanged(
     object sender,
     DependencyPropertyChangedEventArgs args)
    {
      var attachedObject = sender as BindableApplicationBar;
      if (attachedObject != null)
      {
        attachedObject.IsVisible = (bool) args.NewValue;
      }
    }

    #endregion

    #region Attached Dependency Property IsEnabled
    public static readonly DependencyProperty IsEnabledProperty =
         DependencyProperty.RegisterAttached("IsEnabled",
         typeof(bool),
         typeof(BindableApplicationBar),
         new PropertyMetadata(true, IsEnabledChanged));

    // Called when Property is retrieved
    public static bool GetIsEnabled(DependencyObject obj)
    {
      return (bool)obj.GetValue(IsEnabledProperty);
    }

    // Called when Property is set
    public static void SetIsEnabled(
       DependencyObject obj,
       bool value)
    {
      obj.SetValue(IsEnabledProperty, value);
    }

    // Called when property is changed
    private static void IsEnabledChanged(
     object sender,
     DependencyPropertyChangedEventArgs args)
    {
      var attachedObject = sender as BindableApplicationBar;
      if (attachedObject != null)
      {
        attachedObject.IsEnabled = (bool)args.NewValue;
      }
    }
    #endregion

  }
}
